<template>
  <el-main class="main-container">
    <Table :request-data="requestData" v-on:list="listHander" ref="table">
      <template slot="search-box">
        <!-- 关键词 -->
        <el-form-item label="关键词">
          <el-input style="width:100px" size="small" v-model="requestData.postData.keyword"></el-input>
        </el-form-item>
        <!-- 商品价格-->
        <el-form-item label="商品价格" style="margin-left:10px;">
          <el-input style="width:50px" size="small" v-model="requestData.postData.start_price"></el-input>
          <span style="padding-left:5px;padding-right:5px;">-</span>
          <el-input style="width:50px" size="small" v-model="requestData.postData.end_price"></el-input>
        </el-form-item>
        <!-- 游戏名称 -->
        <el-form-item label="游戏区服" style="margin-left:10px;">
          <el-select
            size="small"
            placeholder="请选择"
            style="width:100px"
            v-model="requestData.postData.belongs"
          >
            <el-option label="全部游戏" value="-1"></el-option>
            <el-option v-for="item of games" :label="item.name" :value="item.code"></el-option>
          </el-select>
        </el-form-item>
        <!-- 商品类型 -->
        <el-form-item label="物品类型" style="margin-left:10px;">
          <el-select
            size="small"
            placeholder="请选择"
            style="width:100px"
            v-model="requestData.postData.goods_type"
          >
            <el-option label="全部类型" value="-1"></el-option>
            <el-option v-for="item of goodsType" :label="item.key" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-left:10px;">
          <el-button size="small" @click="search" round>查询</el-button>
        </el-form-item>
        <el-form-item style="margin-left:10px;">
          <el-button size="small" type="danger" @click="sellDialogVisible=true" round>出售</el-button>
        </el-form-item>
      </template>
      <template slot="table-box">
        <el-table :data="tableData" stripe style="width: 100%" height="570">
          <el-table-column label="添加日期" width="170">
            <template slot-scope="scope">
              <span>{{ $utils.formatDateTime(scope.row.add_time)}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="title" label="标题">
            <template slot-scope="scope">
              <a class="link" @click="viewDetail(scope.$index)">
                <span class="one-line">{{scope.row.title}}</span>
              </a>
            </template>
          </el-table-column>

          <el-table-column prop="price" label="价格(积分)" width="120"></el-table-column>
          <el-table-column prop="quantity" label="库存" width="80"></el-table-column>
          <el-table-column prop="belongs" label="游戏区服" width="120">
            <template slot-scope="scope">
              <span>{{ getBelongs(scope.row.belongs)}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="goods_type" label="物品类型" width="120">
            <template slot-scope="scope">
              <span>{{ getGoodsType(scope.row.goods_type)}}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="80">
            <template slot-scope="scope">
              <el-button size="mini" round type="primary" @click="viewDetail(scope.$index)">查 看</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </Table>
    <!--  -->

    <sell-dialog
      :visible="sellDialogVisible"
      :games="games"
      :goodsType="goodsType"
      @close="sellDialogVisible=false"
    />

    <detail-dialog
      v-if="currentIndex!=-1"
      :visible="currentIndex!=-1"
      :data="tableData[currentIndex]"
      :games="games"
      :goodsType="goodsType"
      @close="currentIndex=-1"
    />
  </el-main>
</template>
<script>
import Table from "@/components/Table.vue";
import sellDialog from "./components/sellDialog";
import detailDialog from "./components/DetailDialog";
export default {
  components: {
    Table,
    sellDialog,
    detailDialog
  },
  data() {
    return {
      currentIndex: -1,
      sellDialogVisible: false,
      requestData: {
        urlPath: "/trade/alllist",
        postData: {
          seller: "",
          keyword: "",
          start_price: "",
          end_price: "",
          belongs: "-1",
          goods_type: "-1",
          page_num: 1
        }
      },

      isLoaded: false,
      tableData: [],
      games: [],
      goodsType: []
    };
  },
  methods: {
    listHander(e) {
      this.tableData = e;
      this.isLoaded = true;
    },
    search() {
      this.$refs.table.init();
    },
    getBelongs(belongs) {
      let game = this.games.find(item => {
        return item.code == belongs;
      });
      return game.name;
    },
    getGoodsType(type) {
      let goodsTypeItem = this.goodsType.find(item => {
        return item.value == type;
      });
      return goodsTypeItem.key;
    },
    viewDetail(index) {
      this.currentIndex = index;
    }
  },
  created() {
    this.$http
      .post("/game/list", this.postData)
      .then(res => {
        if (res.code == 200) {
          this.games = res.data.list;
        } else {
          this.$message.error(res.msg);
        }
      })
      .catch(err => {
        this.$message.error(err.msg);
      });
    this.$http
      .post("/trade/goodstype", {})
      .then(res => {
        if (res.code == 200) {
          this.goodsType = res.data;
        } else {
          this.$message.error(res.msg);
        }
      })
      .catch(err => {
        this.$message.error(err.msg);
      });
  }
};
</script>
<style scoped>

</style>